//
//  NWDIscoverViewController.m
//  World
//
//  Created by lanou on 15/10/24.
//  Copyright © 2015年 小虫. All rights reserved.
//

#import "NWDIscoverViewController.h"
#import "NWDIscoverTableViewCell.h"
#import "NWDiscoverDetailViewController.h"
#import "NWRequestManager.h"
#import "NWItemsModle.h"
@interface NWDIscoverViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)UITableView *discoverTableView;  // 主页面
@property(nonatomic, strong)NSMutableArray *dataSourceArray;  // 数据源
@end

@implementation NWDIscoverViewController

// 懒加载
- (NSMutableArray *)dataSourceArray
{
    if (!_dataSourceArray)
    {
        self.dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"发现";
    _discoverTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _discoverTableView.delegate = self;
    _discoverTableView.dataSource = self;
    [self.view addSubview:_discoverTableView];
    
    [_discoverTableView registerClass:[NWDIscoverTableViewCell class] forCellReuseIdentifier:@"reuse"];
    _discoverTableView.rowHeight = 290;
    [self configureData];
    
}

- (void)configureData
{
    
    [NWRequestManager requestWithType:GET urlString:@"http://api.yizhong.cccwei.com/api.php?srv=2001&cid=413000&uid=0&tms=20151024205329&sig=c6f3372607692726&wssig=29c973ed98110c32&os_type=3&version=8&city_id=2&channel=meizu&since_id=0&page_size=20" parDic:nil finish:^(NSData *data) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
        
        
        NSArray *rstArr = dict[@"data"][@"items"];
        for (NSDictionary *dict in rstArr)
        {
            NWItemsModle *modle = [[NWItemsModle alloc]init];
            [modle setValuesForKeysWithDictionary:dict];
            [self.dataSourceArray addObject:modle];
        }
        
        [_discoverTableView reloadData];
        
    } error:^(NSError *error) {
        
        NSLog(@"首页请求数据失败%@",error);
    }];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NWDIscoverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.modle = self.dataSourceArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NWDiscoverDetailViewController *detaVC = [[NWDiscoverDetailViewController alloc]init];
    [self.navigationController pushViewController:detaVC animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
